package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.utils.Androids;

import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyTransferNumber;

/**
 * 资产划转
 */
public class AssetTransferViewModel extends AndroidViewModel {

    private final int uid = Androids.LocalConfig.getUid();
    private final String token = Androids.LocalConfig.getToken();

    private MutableLiveData<Resource<BaseResponseModel>> mTransferLiveData;

    public AssetTransferViewModel(@NonNull Application application) {
        super(application);
        mTransferLiveData = new MutableLiveData<>();
    }

    /**
     * 币币划转到法币
     */
    public MutableLiveData<Resource<BaseResponseModel>> transferToCurrency(String id, String number) {
        if (verifyTransferNumber(number)) {
            double doubleNumber = Double.parseDouble(number);
            DataRepository.get().transferToCurrency(uid, token, id, doubleNumber, result -> {
                if (result.isSucceed()) {
                    mTransferLiveData.setValue(Resource.success(result));
                } else {
                    mTransferLiveData.setValue(Resource.error(result.getMsg()));
                }
            });
        }

        return mTransferLiveData;
    }

    /**
     * 法币划转到币币
     */
    public MutableLiveData<Resource<BaseResponseModel>> transferToLegalTender(String id, String number) {
        if (verifyTransferNumber(number)) {
            double doubleNumber = Double.parseDouble(number);
            DataRepository.get().transferToLegalTender(uid, token, id, doubleNumber, result -> {
                if (result.isSucceed()) {
                    mTransferLiveData.setValue(Resource.success(result));
                } else {
                    mTransferLiveData.setValue(Resource.error(result.getMsg()));
                }
            });
        }

        return mTransferLiveData;
    }
}
